
#pragma warning( push )           // memorize the warning status
#pragma warning (disable:4786)   // disable  identifier was truncated to '255' characters in the debug information


#include "tf_Line.h"

using namespace std;

TfLine::TfLine()
{
}

TfLine::~TfLine()
{

}


void TfLine::GetNodes(IntVector& nodeIds)
{
	 nodeIds = m_NdVec;
}

int  TfLine::GetPartId()
{
	return m_partId;
}



/************************************************************************/
/*                                                                      */
/************************************************************************/
TfLineManager::TfLineManager()
{
	AdditionSpace();
	m_ver = 1;
	
}

TfLineManager::~TfLineManager()
{
	int i;
	for(i = 0; i < m_data.size(); i++)
	{
		delete m_data[i];
		m_data[i] = NULL;
	}
	m_data.clear();

}


int TfLineManager::CreateLine(std::vector<int>& nodeIds, int partId)
{
	int idx,id;
	TfLine tmp;
	
	if(nodeIds.size() > 0)
	{
		tmp.m_NdVec = nodeIds;
		id = tmp.m_id = GetMaxId()+1;
		tmp.m_partId = partId;
	}
	else
		return 0;
	
	
	idx = End();
	AddToDataBase(idx+1,&tmp);
		
	return id;
}


void TfLineManager::DeleteLine(int id)
{
	int idx;

	idx = (*this)[id];
	if(idx > -1)
	{
		m_data[idx]->Delete();
	}
}

void TfLineManager::DeleteLines(IntVector& ids)
{
	for(int i = 0; i < ids.size(); i++)
		DeleteLine(ids[i]);
}


void TfLineManager::GetLineNodes(int id,IntVector& nodeIds)
{
	int idx;
	
	idx = (*this)[id];
	if(idx > -1)
	{
		TfLine* pLn = (TfLine*)(m_data[idx]);
		pLn->GetNodes(nodeIds);
	}
}

int TfLineManager::GetLinePartId(int id)
{
	int idx;
	idx = (*this)[id];
	if(idx > -1)
	{
		TfLine* pLn = (TfLine*)(m_data[idx]);
		return pLn->GetPartId();
	}
	return -1;
}

void TfLineManager::GetPartLines(int pid,IntVector& elems)
{
	int i,end;
	
	end = End();/*may error*/
	if (end <= -1)
	{
		return;
	}
	TfLine* pLn;
	for(i = Begin(); i <= end; i = Next())
	{
		pLn = (TfLine*)(m_data[i]);
		if(pLn->GetPartId() == pid)
			elems.push_back(pLn->GetId());
	}

}

int TfLineManager::GetPartLines(int pid,int* elems )
{
	int i,cnt,end;
	
	if(elems == NULL)
		return -1;
	end = End();/*may error*/
	if (end <= -1)
	{
		return -1;
	}
	TfLine* pLn;
	cnt = 0;
	for(i = Begin(); i <= end; i = Next())
	{
		pLn = (TfLine*)(m_data[i]);
		if(pLn->GetPartId() == pid)
		{
			elems[cnt] = pLn->GetId();
			cnt++;
		}
	}
	return cnt;
}


void TfLineManager::AddToDataBase(int idx, TfGeomObject* obj)
{
	if(idx >-1)
	{
		TfLine* pLn = (TfLine*)(m_data[idx]);
		TfLine* pObj = (TfLine*)obj;
		*pLn = *pObj;

		AddToMap(obj->GetId(),idx);
	}
}

void TfLineManager::AdditionSpace()
{
	int i;
	TfLine* pObj;
	for(i = 0; i < 10000; i++)
	{
		pObj = new TfLine;
		m_data.push_back(pObj);
	}
}

TfLineManager* TfLineManager::Instance()
{
	static TfLineManager lineManager;
	return &lineManager;
}

#pragma warning( pop )           // memorize the warning status